package com.hjzd.ai.smartpractice.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 生成UUID的工具类
 *
 * @author pengyonglei
 * @version 1.0.0
 */
public class RequestIdUtil {

	public static String ERROR_ID = "ERROR_";

	/**
	 * 生成error日志专用的UUID
	 */
	public static String generateErrorUUID() {
		return ERROR_ID + UUID.randomUUID();
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(generateNumberOrderNo());
		System.out.println(URLEncoder.encode("Hjzd@123456", "UTF-8"));
	}

	/**
	 * 生成28位大写订单号
	 */
	public static String generate28OrderNo(){
		return UUID.randomUUID().toString().replaceAll("-","").substring(0,28).toUpperCase();
	}

	/**
	 * 生成32位大写订单号
	 */
	public static String generate32OrderNo(){
		return UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
	}


	public static String generateNumberOrderNo() {
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmmsssss") + getRandomNum(3);
	}

	private static String getRandomNum(int length) {
		String base = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

}
